from PySide6.QtWidgets import QWidget
from PySide6.QtCore import Signal, Qt
from ..layouts.vbox_layout import TgVBoxLayout
from ..inputs.label import TgLabel
from ...style_manager import StyleManager

class NavButton(QWidget):
    clicked = Signal()

    def __init__(self, text, parent=None, size='medium'):
        super().__init__(parent)
        self._is_active = False
        self.setCursor(Qt.CursorShape.PointingHandCursor)

        margins = {
            'high': 'large',
            'medium': 'small',
            'low': 'extra_small'
        }.get(size, 'small')

        self._layout = TgVBoxLayout(self, margins=margins, spacing="small")
        self._label = TgLabel(text)
        self._label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self._layout.addWidget(self._label)

        self.update_style()

    def mousePressEvent(self, event):
        self.clicked.emit()
        super().mousePressEvent(event)

    def set_active(self, active):
        self._is_active = active
        self.update_style()

    def update_style(self):
        if self._is_active:
            background_color = StyleManager.COLORS['primary_light']
            text_color = StyleManager.COLORS['primary']
        else:
            background_color = "transparent"
            text_color = StyleManager.COLORS['text_secondary']

        self.setStyleSheet(f"""
            NavButton {{
                background-color: {background_color};
                border-radius: {StyleManager.BORDER_RADIUS['medium']};
            }}
            NavButton:hover {{
                background-color: {StyleManager.COLORS['background_light']};
            }}
        """)
        self._label.setStyleSheet(f"color: {text_color}; font-weight: bold;")
